package docker

//docker命令
//帮助文档的地址  https://docs.docker.com/engine/reference/run/

/*

-------帮助命令----------

	docker version  //显示docker的版本信息
	docker info  //显示docker的系统信息，包括显示镜像和容器的数量
	docker [命令] --help //帮助命令
		as: docker images --help

	docker stats  //查看cpu的状态


---------特点---------
	Docker镜像都是只读的，当容器启动时，一个新的可写层被加载到镜像的顶部!
	这一层就是我们通常说的容器层，容器之下的都叫镜像层!


--------容器命令----------
//说明：有了镜像才可以创建容器，下载一个centos镜像来学习


		//退出容器 从容器中退出主机
		[root@c7ed261c1b59 /]# exit


		--------docker run----------

		//新建容器并启动
		docker run [可选命令] image

		//可选参数说明
		--name="Name"	//容器名字 app1 app2 用于区分容器  //docker run --name="app" golang
		-d				//后台方式运行
		-it				//使用交互方式运行，进入容器查看内容  //docker run -it centos /bin/bash
		-p				//指定容器的端口
			-p ip:主机端口:容器端口
			-p 主机端口:容器端口  常用  //主机端口映射到容器端口
			-p 容器端口   //容器端口，不往外走
			容器端口 //
		-P				//大写P,随机指定端口
		-e 环境配置修改   //如1核2g服务器，安装elasticsearch，直接卡没，所以需要调整分配给它的内存
						-e ES_JAVA_OPTS="-Xms64m -Xmx510m"  //允许最小内存64m，最大510m

		//	-p/P  端口暴露 查看 images/002docker端口暴露.png

		--restart=always  ##总是启动，当docker重新启动后，这个容器也会跟着启动，而不用再次手动启动




		启动并进入容器centos
		[root@VM_0_13_centos docker]# docker run -it centos /bin/bash

		//注意这里root@后变成了[容器的id]
		[root@c7ed261c1b59 /]# ls	//查看容器内部的centos，基础班，很多命令都不完善

		//该容器内就是liunx centos环境
		bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

		//退出容器 从容器中退出主机
		[root@c7ed261c1b59 /]# exit
		exit
		[root@VM_0_13_centos docker]#







		--------docker ps 列出容器----------

		列出所有的运行的容器
		docker ps [可选命令]
					//列出当前正在运行的容器
			-a		//列出当前正在运行的容器+带出历史运行过的容器
			-n=?	//?为整数，显示最近创建的容器  //-n=1 ，显示最近一个创建的容器
			-q  	//只显示容器的编号



		--------exit 退出容器----------

		退出容器
			exit   			//直接容器停止并退出
			Ctrl + P + Q 	//容器不停止退出


		--------docker rm 删除容器----------

		删除容器
		docker rm 容器id					//删除指定的容器，不能删除正在运行中的容器，如果要强制删除 rm -f
		docker rm -f $(docker ps -aq)	//删除所有的容器
		docker ps -a -q|xargs docker rm //删除所有的容器




		--------启动和停止容器的操作----------

		docker start 容器id  	//启动
		docker restart 容器id	//重启
		docker stop 容器id		//停止当前正在运行的容器
		docker kill	容器id		//强制停止当前正在运行的容器



		--------后台启动容器----------
		docker run -d 镜像名

			docker run -d centos	[-it]是前台进程
			//问题 docker ps 发现centos停止了
			//常见的坑，docker 容器使用[后台运行]，就必须要有一个[前台进程]，docker发现没有应用，就会自动停止
			//nginx容器启动后，发现自己没有提供服务，就会立刻停止，就是没有程序了



		---------进入当前正在运行的容器----------

		//我们通常容器都是使用后台方式运行的，需要进入容器，修改一些配置
		//进入容器后，开启一个新的终端，可以在里面操作
		docker exec -it 容器id bashShell
			docker exec -it 容器id  /bin/bash
			-it 以交互模式进行


		//进入容器正在执行过的终端，不会启动新的进程
		docker attach 容器id
			docker attach  容器id  /bin/bash
			//正在执行的代码...


		docker exec  与  docker attach 的区别

			docker exec  	//进入容器后，开启一个新的终端，可以在里面操作
			docker attach 	//进入容器正在执行的终端，不会启动新的进程



		---------docker cp :用于容器与主机之间的数据拷贝。----------
		//从主机拷贝到容器内，一般用挂载

		docker cp 容器id:容器内路径   目的的主机路径
			docker cp 容器id:/home/test.txt   /home





----docker exec -it ---------

[root@VM_0_13_centos ~]# docker run -it -d golang /bin/bash
d067308ea4bc009e4f58555e69799e165be249be4faf68faa985f6d241979a84
[root@VM_0_13_centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d067308ea4bc   golang    "/bin/bash"              15 seconds ago   Up 14 seconds             funny_kepler
e6d3268bb0bc   nginx     "/docker-entrypoint.…"   42 minutes ago   Up 42 minutes   80/tcp    laughing_blackburn
[root@VM_0_13_centos ~]#

[root@VM_0_13_centos ~]# docker exec -it d067308ea4bc /bin/bash
root@d067308ea4bc:/go# go version
go version go1.17.5 linux/amd64
root@d067308ea4bc:/go#
root@d067308ea4bc:/go# exit
exit
[root@VM_0_13_centos ~]#









# docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to
                                       disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup
                                       namespace
                                       'private': Run the container in its own private cgroup
                                       namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
  -d, --detach                         Run container in background and print container ID
      --detach-keys string             Override the key sequence for detaching a container
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --domainname string              Container NIS domain name
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --gpus gpu-request               GPU devices to add to the container ('all' to pass all GPUs)
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before starting
                                       health-retries countdown (ms|s|m|h) (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and
                                       reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable
                                       unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --platform string                Set platform if server is multi-platform capable
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container's port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --pull string                    Pull image before running ("always"|"missing"|"never")
                                       (default "missing")
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container
[root@VM_0_13_centos ~]#



*/
